//package com.sixbro.resource.component;
//
//import com.google.common.collect.ImmutableBiMap;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.security.access.AccessDeniedException;
//import org.springframework.security.authentication.InsufficientAuthenticationException;
//import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
//import org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint;
//import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
//import org.springframework.security.oauth2.server.resource.InvalidBearerTokenException;
//
//import java.time.LocalDateTime;
//
///**
// * <p>
// *
// * </p>
// *
// * @author: Mr.Lu
// * @since: 2021/11/2 16:39
// */
//public class OAuth2WebResponseExceptionTranslator implements WebResponseExceptionTranslator< Object > {
//
//    public OAuth2AuthenticationEntryPoint authenticationEntryPoint( ) {
//        OAuth2AuthenticationEntryPoint point = new OAuth2AuthenticationEntryPoint( );
//        point.setExceptionTranslator( this );
//        return point;
//    }
//
//    public OAuth2AccessDeniedHandler accessDeniedHandler( ) {
//        OAuth2AccessDeniedHandler handler = new OAuth2AccessDeniedHandler( );
//        handler.setExceptionTranslator( this );
//        return handler;
//    }
//
//    @Override
//    public ResponseEntity< Object > translate(Exception e ) {
//        String name = e.getClass( ).getName( );
//        String path = String.format( "at %s", e.getStackTrace( )[ 0 ] );
//        HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
//        System.err.println( name );
//        System.err.println( "\t" + path );
//        if ( e instanceof InsufficientAuthenticationException) {
//            httpStatus = HttpStatus.UNAUTHORIZED;
//        }
//        if ( e instanceof InvalidBearerTokenException) {
//            httpStatus = HttpStatus.NOT_ACCEPTABLE;
//        }
//        if ( e instanceof AccessDeniedException) {
//            httpStatus = HttpStatus.FORBIDDEN;
//        }
//        return ResponseEntity.status( httpStatus ).body( ImmutableBiMap.of(
//                "message", e.getMessage( ) == null ? "" : e.getMessage( ),
//                "timestamp", LocalDateTime.now( ).toString( ),
//                "error", name,
//                "path", path
//        ) );
//    }
//}